/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rentcar.services;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import static rentcar.datareceiver.Application.g_SocketList;

/**
 *
 * @author wuhao
 */
public class MySocketServer implements Runnable {

        private static final int port = 9876;

        boolean SocketIsConnected(Socket socket) {
                try {
                        socket.sendUrgentData(0xFF);
                        return true;
                } catch (Exception ex) {
                        return false;
                }
        }

        @Override
        public void run() {
                System.out.println("Start listening...");
                //服务端在9876端口监听客户端请求的TCP连接   
                ServerSocket server = null;
                try {
                        server = new ServerSocket(port);
                } catch (IOException ex) {
                        Logger.getLogger(MySocketServer.class.getName()).log(Level.SEVERE, null, ex);
                }
                Socket client = null;
                //通过调用Executors类的静态方法，创建一个ExecutorService实例  
                //ExecutorService接口是Executor接口的子接口  
                Executor service = Executors.newCachedThreadPool();

                boolean f = true;
                while (f) {
                        try {
                                //等待客户端的连接
                                client = server.accept();
                                g_SocketList.add(client);
                                System.out.println("client connected!");

                                //删除已断开的连接
                                for (int i = 0; i < g_SocketList.size(); i++) {
                                        if (g_SocketList.get(i) == null || !SocketIsConnected(g_SocketList.get(i))) {
                                                g_SocketList.remove(i);
                                                i--;
                                        }
                                }

                                System.out.println("g_SocketList 个数：" + g_SocketList.size());
                        } catch (IOException ex) {
                                Logger.getLogger(MySocketServer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        //System.out.println("与客户端连接成功！");
                        //调用execute()方法时，如果必要，会创建一个新的线程来处理任务，但它首先会尝试使用已有的线程，  
                        //如果一个线程空闲60秒以上，则将其移除线程池；  
                        //另外，任务是在Executor的内部排队，而不是在网络中排队  
                        service.execute(new ServerThread(client));
                }
                try {
                        server.close();
                } catch (IOException ex) {
                        Logger.getLogger(MySocketServer.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
}
